// 导入依赖模块
yepjs.require("jquery");
yepjs.require("YBA.Plugin");

/*var ebeimApiUrl = ctx+"/api";*/
/*var ebeimApiUrl = "http://192.168.1.101:8080/ebeim-api";*/

yepjs.define("ajax", {

    NS: "YBA.Data",

    field: function (options, callback) {

        var callee = arguments.callee, arg = arguments, status = "success";

        // 默认的ajax请求参数
        var defOptions = {
            dataType: "json",
            type: "POST",
            timeout: 100000000,
            complete: function () {
                YBA.Plugin.loading.hide();
                status = arguments[1];
                if(arguments[1] === "success"){
                    callback && callback();
                }
            }
        };

        var op = $.extend(defOptions, options);

        // 默认显示loading状态，除非传入noloading参数
        if(!op.noloading){
            YBA.Plugin.loading.show(op.loadingTxt || "请求中，请稍候");
        }

        var success = op.success;
        var error = op.error;

        op.success = function (json) {
            /*if(!json.islogin){
                location.href="";
                return;
            }*/
            success(json);
        };

        op.error = function () {
            YBA.Plugin.loading.hide();
            var confirm = YBA.Plugin.confirm({
                msg: "很抱歉，网络请求出错了",
                btnTxt1: "重试",
                onHide: function () {
                    if(status !== "success") {
                        error && error();
                    }
                },
                yes: function () {
                    confirm.hide(false);
                    callee.apply(null, arg);
                }
            });
        };

        delete op.noloading;
        
        op.url = ctx+"/api" + op.url + "?timestamp="+ new Date().getTime();
        
        $.ajax(op);
    }

});
